/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.room.phom.PhomExtension;
import extension.room.phom.PhomGame;
import extension.room.phom.PhomPlayer;
import extension.room.phom.PhomResponse;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class GetCardHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        PhomExtension phomExtension = (PhomExtension) getParentExtension();
        PhomResponse response = phomExtension.getResponse();
        PhomGame phomGame = phomExtension.getGame();
        PhomPlayer p = phomGame.getPlayerFight();
        if (user.getName().equals(p.getName()) && p.getNumberCardFights() < 10 && phomGame.isCanGetDrawCard()) {
            int drawCard = phomGame.getDrawCard();
            Debug.d(user.getName() + "- get card :" + drawCard);
            if (drawCard != -1) {
                int canU = 0;
                p.generateListPhom();
                List<Integer> listPhom = p.getListPhom();
                if (listPhom.size() >= 9) {
                    canU = 1;
                }
                response.sendDrawCard(drawCard, p.getListPhom(), user);
                response.sendDrawCardTurn(phomGame.getGameTurn(), canU);
                if (canU == 1) {
                    phomGame.onFinishU(PhomGame.HAVE_PHOM);
                }
                if (p.getNumberCardFights() >= phomGame.getNumberCardFinish() - 1 && phomGame.getIsPlay()) {
                    p.clearPhomFinal();
                    phomGame.setLastShowPhom();
                    if (!p.getIsMoreFightTurn()) {
                        phomExtension.getResponse().showPhom(p.getHavePhom(), p.getListPhom(), p.getSeatPos(),
                                user);
                    } else {
                        phomExtension.getResponse().showPhomMore(p.getHavePhom(), p.getListPhomMore(), p.getSeatPos(),
                                user);
                    }
                }
            }
        }
    }
}
